package com.ctgu.server.controller;


import com.ctgu.server.dao.dto.LoginDto;


import com.ctgu.server.dao.dto.UserDto;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.dao.vo.UserVo;
import com.ctgu.server.service.FormService;
import com.ctgu.server.service.UserService;
import com.ctgu.server.utils.ResultUtil;
import com.ctgu.server.utils.WeiXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ctgu
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/server/user")
@CrossOrigin
@Api(tags = "用户操作接口")
public class UserController {
    @Autowired
    private UserService userService;

//    @Autowired
//    private CacheChannel cacheChannel;
//
//    @Autowired
//    private ValidateCodeService validateCodeService;

    @Autowired
    private FormService formService;

    @PostMapping("/login")
    @ApiOperation(notes = "登录接口",value = "login")
    public Result Login(@Validated @RequestBody LoginDto loginDto){
         Map map=WeiXinUtil.getLogin(loginDto.getJscode());
        System.out.println("用户请求数据：  "+map);
        return userService.UserLogin(map);
    }

//    @GetMapping(value = "/captcha",produces = "image/png")
//    @ApiOperation(notes = "验证码",value = "captcha")
//    public void captcha(@RequestParam(value = "key") String key, HttpServletResponse response) throws IOException {
//        validateCodeService.create(key,response);
//    }
    //效验验证码
//    @PostMapping("/check")
//    @ApiOperation(notes = "效验验证码",value = "check")
//    public Result check( @RequestBody LoginParamDTO loginParamDTO){
//        if(!validateCodeService.check(loginParamDTO.getKey(), loginParamDTO.getCode())){
//            return ResultUtil.error("验证码错误");
//        }
//        //效验验证码正确与否
//        return ResultUtil.success();
//    }

//    @PostMapping("/register")
//    @ApiOperation(notes = "注册新用户",value = "注册新用户")
//    public Result UserRegister(@Validated @RequestBody RegisterDto registerDto){
//        return userService.userRegister(registerDto);
//    }

    @PutMapping("/update")
    @ApiOperation(notes = "更新用户信息",value = "update")
    public Result updateUser(@Validated @RequestBody UserDto user){

        return userService.updateByUserId(user);
    }

    @GetMapping("/userInfo/{id}")
    @ApiOperation(notes = "获取用户信息",value = "getUserInfo")
    public Result getUserInfo(@PathVariable String id){
        return userService.getUserInfo(id);
    }

    @PostMapping("/upload/avatar")
    @ApiOperation(notes = "uploadAvatar",value = "uploadAvatar")
    public Result uploadAvatar(@RequestParam String id, @RequestParam MultipartFile multipartFile){
        if(multipartFile==null||id==null){
            return ResultUtil.error("参数错误！"+id);
        }
        return userService.uploadUserAvatar(id,multipartFile);

    }

//    @GetMapping("/getForm")
//    @ApiOperation(notes = "获取用户表单信息",value = "getForm")
//    public Result uploadAvatar(@NotEmpty(message = "Id不能为空") @RequestParam String id){
////        formService.getById()
//
//
//    }

}

